package com.gugee.ins.common.dal.service.brand.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gugee.ins.common.dal.mapper.brand.InsRelBrandMediaMapper;
import com.gugee.ins.common.dal.service.brand.InsRelBrandMediaDao;
import com.gugee.ins.common.model.mysql.brand.InsRelBrandMedia;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class InsRelBrandMediaDaoImpl extends ServiceImpl<InsRelBrandMediaMapper, InsRelBrandMedia> implements InsRelBrandMediaDao {

    @Transactional(rollbackFor=Exception.class)
    @Override
    public void delAndInsert(int bid, List<InsRelBrandMedia> insRelBrandMedias,int saveSize) {
        baseMapper.deleteByBid(bid);
        int totalPage = insRelBrandMedias.size()%saveSize > 0
                ?insRelBrandMedias.size()/saveSize+1
                :insRelBrandMedias.size()/saveSize;
        for (int i = 0; i < totalPage; i++) {
            int startIndex = i*saveSize;
            int endIndex = startIndex + saveSize;

            if(endIndex >insRelBrandMedias.size()){
                endIndex = insRelBrandMedias.size();
            }
            this.insertBatch(insRelBrandMedias.subList(startIndex,endIndex));
        }
    }

    @Override
   public void insertBatch(List<InsRelBrandMedia> insRelBrandMedias){
        baseMapper.insertBatch(insRelBrandMedias);
   }
}
